var jc_data = require('../../../../utils/jc.js');

Page({
  data: {
    scrollAZ: null,
    scrollNow: 0,
    cityType: 'begin',
    cityResults: null, 
    code: null,
    isShowLayer: false,
    cityAZ: [{
      cityName: 'A'
    }, {
      cityName: 'B'
    }, {
      cityName: 'C'
    }, {
      cityName: 'D'
    }, {
      cityName: 'E'
    }, {
      cityName: 'F'
    }, {
      cityName: 'G'
    }, {
      cityName: 'H'
    }, {
      cityName: 'I'
    }, {
      cityName: 'J'
    }, {
      cityName: 'K'
    }, {
      cityName: 'L'
    }, {
      cityName: 'M'
    }, {
      cityName: 'N'
    }, {
      cityName: 'O'
    }, {
      cityName: 'P'
    }, {
      cityName: 'Q'
    }, {
      cityName: 'R'
    }, {
      cityName: 'S'
    }, {
      cityName: 'T'
    }, {
      cityName: 'U'
    }, {
      cityName: 'V'
    }, {
      cityName: 'W'
    }, {
      cityName: 'X'
    }, {
      cityName: 'Y'
    }, {
      cityName: 'Z'
    },],
    citys: jc_data.jc_data
  },
  onLoad: function (options) {
    var that = this;
    var title = '';
    if (options.cityType == "begin") {
      title = '始发地'
    }
    if (options.cityType == "end") {
      title = '目的地'
    }
    wx.setNavigationBarTitle({
      title: title
    });
    that.setData({
      cityType: options.cityType
    });
    if (that.data.cityResults == null) {
      that.setData({
        cityResults: this.data.citys
      })
    }
  },
  bindAZ: function (e) {
    var currentCityName = e.currentTarget.dataset.id
    var that = this;
    //放入A-Z的scrollTop参数
    if (that.data.scrollAZ == null) {
      wx.createSelectorQuery().selectAll('.city-item-A-Z').fields({
        dataset: true,
        size: true,
        rect: true
      }, function (res) {
        res.forEach(function (re) {
          if (currentCityName == re.dataset.cityname) {
            wx.pageScrollTo({
              scrollTop: re.top + that.data.scrollNow - 110 / 750 * wx.getSystemInfoSync().windowWidth,
              duration: 0
            })
          }
        })
      }).exec();
    } else {
      that.data.scrollAZ.forEach(function (re) {
        if (currentCityName == re.dataset.cityname) {
          wx.pageScrollTo({
            scrollTop: re.top + that.data.scrollNow - 110 / 750 * wx.getSystemInfoSync().windowWidth,
            duration: 0
          })
        }
      })
    }

    //提示点击的字母
    that.setData({
      code: currentCityName,
      isShowLayer: true
    });

    setTimeout(() => {
      that.setData({ isShowLayer: false })
    }, 500);
  },
  onPageScroll: function (e) { // 获取滚动条当前位置
    this.setData({
      scrollNow: e.scrollTop
    })
  },
  citySelected: function (e) {
    var that = this;
    var cityNameTemp = e.currentTarget.dataset.cityname
    var pages = getCurrentPages(); // 获取页面栈
    var prevPage = pages[pages.length - 2];
    if (that.data.cityType == 'begin') {
      prevPage.setData({
        origin: cityNameTemp
      });
    }
    if (that.data.cityType == "end") {
      prevPage.setData({
        destination: cityNameTemp
      });
    }
    wx.navigateBack({
      delta: 1
    });
  },
  bindSarchInput: function (e) {
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })

    var that = this;
    var inputVal = e.detail.value;
    var cityResultsTemp = new Array()
    var citys = that.data.citys;

    if (inputVal == null || inputVal.trim() == '') {
      that.setData({
        cityResults: citys
      })
      return;
    }

    for (var i = 0; i < citys.length; i++) {
      if (citys[i].cityName.indexOf(inputVal) == 0 || citys[i].cityPY.toLowerCase().indexOf(inputVal.toLowerCase()) == 0 || citys[i].cityPinYin.toLowerCase().indexOf(inputVal.toLowerCase()) == 0) {
        //去除a-z
        if (citys[i].isTitle) {
          continue;
        }
        var ifHas = false;
        for (var j = 0; j < cityResultsTemp.length; j++) {
          if (cityResultsTemp[j] == citys[i]) {
            ifHas = true;
            break;
          }
        }
        if (!ifHas) {
          cityResultsTemp.push(citys[i]);
        }
      }
    }
    that.setData({
      cityResults: cityResultsTemp
    })
  },
  onPullDownRefresh: function () {
    setTimeout(function () {
      wx.stopPullDownRefresh();
    }, 1000)
  }
})